package com.workingpub.commons.jdbc;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.transaction.annotation.Transactional;

import com.workingpub.commons.utils.Page;

/**
 * JDBC操作管理器：查询、分页、简单ORM等
 * 
 * @author taoping
 *
 */
public interface JdbcManager {

	static final String QUERY_SINGLE_TABLE_YES = "YES";
	static final String QUERY_SINGLE_TABLE_NO = "NO";
	static final String QUERY_DEFAULT_SINGLE_TABLE = QUERY_SINGLE_TABLE_NO;

	<T extends Object> Page<T> queryForPage(String querySql, RowMapper<T> rowMapper, int pageNumber, int pageSize);

	<T extends Object> Page<T> queryForPage(String querySql, Map<String, Object> queryParam, RowMapper<T> rowMapper,
			int pageNumber, int pageSize);

	<T extends Object> Page<T> queryForPage(String querySql, List<Object> queryParam, RowMapper<T> rowMapper,
			int pageNumber, int pageSize);

	<T extends Object> Page<T> queryForPage(String querySql, String countSql, RowMapper<T> rowMapper, int pageNumber,
			int pageSize);

	<T extends Object> Page<T> queryForPage(String querySql, String countSql, Map<String, Object> queryParam,
			RowMapper<T> rowMapper, int pageNumber, int pageSize);

	<T extends Object> Page<T> queryForPage(String querySql, String countSql, List<Object> queryParam,
			RowMapper<T> rowMapper, int pageNumber, int pageSize);

	<T extends Object> List<T> queryForList(String querySql, RowMapper<T> rowMapper);

	<T extends Object> List<T> queryForList(String querySql, Map<String, Object> queryParam, RowMapper<T> rowMapper);

	<T extends Object> List<T> queryForList(String querySql, List<Object> queryParam, RowMapper<T> rowMapper);

	<T extends Object> List<T> queryForList(String querySql, RowMapper<T> rowMapper, Object... queryParam);

	/**
	 * 查询单行实体记录,如果记录数大于1抛出异常
	 * 
	 * @param <T>
	 * @param querySql
	 * @param rowMapper
	 * @return
	 */
	<T extends Object> T queryForObject(String querySql, RowMapper<T> rowMapper);

	/**
	 * 查询单行实体记录,如果记录数大于1抛出异常
	 * 
	 * @param <T>
	 * @param querySql
	 * @param rowMapper
	 * @return
	 */
	<T extends Object> T queryForObject(String querySql, Map<String, Object> queryParam, RowMapper<T> rowMapper);

	/**
	 * 查询单行实体记录,如果记录数大于1抛出异常
	 * 
	 * @param <T>
	 * @param querySql
	 * @param rowMapper
	 * @return
	 */
	<T extends Object> T queryForObject(String querySql, List<Object> queryParam, RowMapper<T> rowMapper);

	/**
	 * 查询单行实体记录,如果记录数大于1抛出异常
	 * 
	 * @param <T>
	 * @param querySql
	 * @param rowMapper
	 * @param queryParam
	 * @return
	 */
	<T extends Object> T queryForObject(RowMapper<T> rowMapper, String querySql, Object... queryParam);

	String queryForString(String querySql, Object... queryParam);

	String queryForString(String querySql, Map<String, Object> queryParam);

	Integer queryForInteger(String querySql, Object... queryParam);

	Integer queryForInteger(String querySql, Map<String, Object> queryParam);

	Long queryForLong(String querySql, Object... queryParam);

	Long queryForLong(String querySql, Map<String, Object> queryParam);

	Date queryForDate(String querySql, Object... queryParam);

	Date queryForDate(String querySql, Map<String, Object> queryParam);

	BigDecimal queryForBigDecimal(String querySql, Object... queryParam);

	BigDecimal queryForBigDecimal(String querySql, Map<String, Object> queryParam);

	int update(String sql, Map<String, ?> paramMap) throws DataAccessException;

	@Transactional
	int update(String sql, Object... args) throws DataAccessException;

	@Transactional
	int update(String sql, Object[] args, int[] argTypes) throws DataAccessException;

	@Transactional
	int[] batchUpdate(String sql, Map<String, ?>[] batchValues);

}
